package com.jy.rhin.support.notification.convertor;


import com.jy.rhin.sdk.dto.notification.resp.PublishMessageDto;
import org.oasis_open.docs.wsn.b_2.NotificationMessageHolderType;
import org.oasis_open.docs.wsn.b_2.Notify;
import org.oasis_open.docs.wsn.b_2.TopicExpressionType;

import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
import java.util.Collections;

/**
 * @Description:
 * @Author: lww
 * @Date: 2022/7/7 15:23
 */
public abstract class BaseNotificationConverter implements NotificationConvert {

    /**
     * 消息转换
     *
     * @param publishMessage
     * @return
     */
    @Override
    public Notify convertMessage(PublishMessageDto publishMessage) {
        Notify notify = new Notify();
        NotificationMessageHolderType notificationMessage = new NotificationMessageHolderType();
        W3CEndpointReferenceBuilder subscriptionReference = new W3CEndpointReferenceBuilder();
        subscriptionReference.address(publishMessage.getSubscribeUid());
        notificationMessage.setSubscriptionReference(subscriptionReference.build());
        W3CEndpointReferenceBuilder producerReference = new W3CEndpointReferenceBuilder();
        producerReference.address(publishMessage.getAddressUrl());
        notificationMessage.setProducerReference(producerReference.build());
        TopicExpressionType topicExpressionType = new TopicExpressionType();
        topicExpressionType.setDialect(publishMessage.getTopicExp());
        notificationMessage.setTopic(topicExpressionType);
        notificationMessage.setMessage(buildMessageContent(publishMessage));
        notify.setNotificationMessage(Collections.singletonList(notificationMessage));
        return notify;
    }

    /**
     * 消息内容打包
     *
     * @param publishMessage
     * @return
     */
    abstract String buildMessageContent(PublishMessageDto publishMessage);
}
